package com.redoor.app.stake.basicdata.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.redoor.app.stake.auth.commons.StakeApplicationStatus;
import com.redoor.app.stake.basicdata.model.BaseEquipmentInfo;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseEquipmentInfoActionBean;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseEquipmentInfoCriteria;
import com.redoor.app.stake.basicdata.model.serviceModel.BaseEquipmentInfoServiceBean;
import com.redoor.app.stake.basicdata.service.BaseEquipmentInfoService;
import com.redoor.app.stake.common.controller.BaseController;
import com.redoornetwork.framework.core.response.ResponseResult;
import com.redoornetwork.framework.core.response.ResultGenerator;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* Created by wangjunlei on 2018-03-12 17:30:28.
 * 设备管理
*/
@RestController
@RequestMapping("/base/equipment/infos")
public class BaseEquipmentInfoController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(BaseEquipmentInfoController.class);
    @Autowired
    private BaseEquipmentInfoService baseEquipmentInfoService;

    /**
     * 设备型号新增
     * @Author zhangYiHang
     * @Date 2018/3/21 20:57
     * @Param
     */
    @ApiOperation(value = "(桩)设备型号新增")
    @RequiresPermissions("base:version:insert")
    @PostMapping("/insert")
    public ResponseResult add(BaseEquipmentInfoActionBean baseEquipmentInfoActionBean) {
        try {
            int status = baseEquipmentInfoService.insertEquipmentInfo(baseEquipmentInfoActionBean);
            //判断添加语句是否执行,如果执行则大于0,返回成功
            if (status > 0) {
                logger.info("添加数据完成");
                return ResultGenerator.generatorSuccess();
            } else {
                return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }
    /**
     * 状态删除数据
     * @Author zhangYiHang
     * @Date 2018/3/21 21:11
     * @Param
     */
    @ApiOperation(value = "状态删除,返回状态:负数为绑定桩数量,正数逻辑删除数量")
    @ApiImplicitParam(name = "id", value = "主键", paramType = "query", dataType = "String", required = true, defaultValue = "")
    @RequiresPermissions("base:version:delete")
    @PostMapping("/del")
    public ResponseResult delete(String id) {
        try {
           int status = this.baseEquipmentInfoService.updateStatus(id);
                return ResultGenerator.generatorSuccess(status);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("执行状态修改异常",e);
            return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }

    /**
     *设备型号修改
     * @Author zhangYiHang
     * @Date 2018/3/21 20:57
     * @Param
     */
    @ApiOperation(value = "(桩)设备型号修改")
    @RequiresPermissions("base:version:update")
    @PostMapping("/update")
    public ResponseResult update(BaseEquipmentInfoActionBean baseEquipmentInfoActionBean) {
        try {
            int status = this.baseEquipmentInfoService.updateEquipmentInfoByUuid(baseEquipmentInfoActionBean);
            //判断是否修改成功如果大于零为修改成功
            if (status > 0) {
                logger.info("数据修改完成");
                return ResultGenerator.generatorSuccess();
            } else {
                return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
            }
        } catch (Exception e) {
            logger.error("修改语句异常",e);
            return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }
    /**
     * 根据主键查询对象
     * @Author zhangYiHang
     * @Date 2018/3/21 21:19
     * @Param
     */
    @ApiOperation(value = "根据主键查询对象,出入id='主键'")
    @PostMapping("/detail")
    public ResponseResult detail(String id) {
        BaseEquipmentInfoServiceBean baseEquipmentInfoServiceBean = this.baseEquipmentInfoService.qryEquipmentInfoByUuid(id);
        return ResultGenerator.generatorSuccess(baseEquipmentInfoServiceBean);
    }
    /**
     * 主列表查询
     * @Author zhangYiHang
     * @Date 2018/3/21 18:16
     * @Param
     */
    @ApiOperation(value = "主列表查询")
    @RequiresPermissions("base:version:list")
    @GetMapping("/list")
    public ResponseResult list(BaseEquipmentInfoCriteria criteria) {
        List<BaseEquipmentInfoServiceBean> list = this.baseEquipmentInfoService.qryEquipmentInfo(criteria);
        Long total = this.baseEquipmentInfoService.qryEquipmentInfoTotal(criteria);
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(list);
        pageInfo.setTotal(total);
        return ResultGenerator.generatorSuccess(pageInfo);
    }
    /**
     * 根据名称查询对象
     * @Author zhangYiHang
     * @Date 2018/3/22 21:11
     * @Param
     */
    @ApiOperation(value = "根据名称查询同一是否存在,当返回为0为不存在,返回其他为存在")
    @PostMapping("/qryEquipmentInfoCountByName")
    public ResponseResult qryEquipmentInfoCountByName(String name, String vendorUuid){
        Integer count = this.baseEquipmentInfoService.qryEquipmentInfoCountByName(name,vendorUuid);
        return ResultGenerator.generatorSuccess(count);
    }

    /**
     * 根据厂商uuid查询设备型号基本信息
     * zhangYiHang
     * @Date 2018/3/22 11:52
     * @param vendorUuid
     * @return
     */
    @ApiOperation(value = "根据厂商uuid查询设备型号基本信息")
    @PostMapping("/qryEquipmentInfoByvendorUuid")
    public ResponseResult qryEquipmentInfoByVendorUuid(String vendorUuid){
        List<BaseEquipmentInfo> list = this.baseEquipmentInfoService.qryEquipmentInfoByVendorUuid(vendorUuid);
        return ResultGenerator.generatorSuccess(list);
    }

}
